package com.cdv.ns.weiwei.servers.app;

import java.io.IOException;
import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.cdv.nsite.common.CommonProperties;
import com.mongodb.MongoClientURI;

@DependsOn("NSite")
@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = { "com.cdv.ns.weiwei.servers.config" })
@ServletComponentScan(value = {"com.cdv.ns.weiwei.filter"})
@ImportResource({ "classpath:com/cdv/ns/weiwei/weiwei-svc-base.xml" })
public class ApplicationWeiWei {
	public static boolean onlyReIndex(String[] args) {
		return false;
	}
	
	@Value("${ns.mongo.weiweiDbUri:mongodb://127.0.0.1:27017/nsitedb_v3}")
	private String weiweiDbUri;
	
	@Value("${es.mongo.nsiteDbUri:mongodb://127.0.0.1:27017/nsitedb_v3}")
	private String nsiteDbUri;
	
	@Bean
	@Primary
	public MongoDbFactory newsphereDbFactory() throws UnknownHostException {
		return new SimpleMongoDbFactory(new MongoClientURI(weiweiDbUri));
	}
	
	@Bean
	public MongoDbFactory nsiteDbFactory() throws UnknownHostException {
		return new SimpleMongoDbFactory(new MongoClientURI(nsiteDbUri));
	}

	@Bean("weiweiMongoTemplate")
	@Primary
	public MongoTemplate weiweiMongoTemplate() throws Exception {
		return new MongoTemplate(this.newsphereDbFactory());
	}

	@Bean("mongoTemplate-nSite")
	public MongoTemplate nsiteMongoTemplate() throws Exception {
		return new MongoTemplate(this.nsiteDbFactory());
	}
	
	public static void main(String[] args) throws IOException {
		boolean onlyShowVer = false;
		for (String s : args) {
			if ("-v".equals(s) || "-version".equals(s) || "--version".equals(s)) {
				onlyShowVer = true;
				break;
			}
		}

		if (onlyShowVer) {
			System.out.println("Weiwei version: " + CommonProperties.getVersion(ApplicationWeiWei.class));
			System.out.println("nSite version: " + CommonProperties.getVersion(CommonProperties.class));
			return;
		}
		if (!onlyReIndex(args)) {
			System.setProperty("dubbo.shutdown.hook", "true");
			String loc = CommonProperties.loadProperties2System(System.getProperty("spring.config.location"));
			System.getProperties().setProperty("application.version",
					CommonProperties.getVersion(ApplicationWeiWei.class));
			System.getProperties().setProperty("app.home", loc + "/..");

			SpringApplication.run(ApplicationWeiWei.class, args);
		}
	}
}
